import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_web_study/bean/menu_item.dart';
import 'package:flutter_web_study/provider/base_provider.dart';
import 'package:flutter_web_study/router/application.dart';
import 'package:flutter_web_study/view_model/commodity_management_view_model.dart';
import '../router/routes.dart';
import 'weight/select_widget.dart';
import 'package:provider/provider.dart';

///@Description 商品管理页面
///@systemUser HXC182
///@Author Nine Tailed Fox
///@Date 2023/1/31 17:25
class CommodityManagementPage extends StatefulWidget {
  const CommodityManagementPage({Key? key}) : super(key: key);

  @override
  State<CommodityManagementPage> createState() =>
      _CommodityManagementPageState();
}

class _CommodityManagementPageState extends State<CommodityManagementPage> {
  @override
  Widget build(BuildContext context) {
    return ProviderWidget<CommodityManagementViewModel>(
        model: CommodityManagementViewModel(context),
        onModelReady: (value) {
          value.initView();
        },
        builder: (context, viewModel, child) {
          return Scaffold(
            appBar: AppBar(
              title: const Text("商品管理"),
            ),
            body: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Row(
                  children: [
                    itemIput("品牌名称:", viewModel.brandNameEidtText),
                    Row(
                      children: [
                        Text("商品分类:"),
                        SelectWidget(
                          items: brandTypeList,
                          value: viewModel.menuItemBean.label,
                          valueChanged: (value) =>
                              viewModel.getMenuItemBean(value),
                        )
                      ],
                    )
                  ],
                ),
                Row(
                  children: [
                    itemIput("系列名称:", viewModel.seriesNameEidtText),
                    itemIput("色号名称:", viewModel.colorNameEidtText),
                  ],
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: MaterialButton(
                      child: Text("查询"),
                      textColor: Colors.white,
                      color: Colors.blue,
                      onPressed: () {
                        viewModel.queryDate();
                      }),
                ),
                Row(
                  children: [
                    Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: MaterialButton(
                          child: Text("新建商品"),
                          textColor: Colors.white,
                          color: Colors.blue,
                          onPressed: () {
                            viewModel.newShopJump();
                          }),
                    ),
                    Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: MaterialButton(
                          child: Text("全部删除"),
                          textColor: Colors.white,
                          color: Colors.blue,
                          onPressed: () {}),
                    )
                  ],
                ),
                Expanded(
                  child: SingleChildScrollView(
                    scrollDirection: Axis.vertical,
                    child: Container(
                      child: DataTable(
                        showCheckboxColumn: true,
                        dividerThickness: 2,
                        sortColumnIndex: 1,
                        columns: const [
                          DataColumn(label: Expanded(child: Text("编号"))),
                          DataColumn(label: Expanded(child: Text("品牌中文名称"))),
                          DataColumn(label: Expanded(child: Text("品牌英文名称"))),
                          DataColumn(label: Expanded(child: Text("商品分类"))),
                          DataColumn(label: Expanded(child: Text("商品系列"))),
                          DataColumn(label: Expanded(child: Text("色号"))),
                          DataColumn(label: Expanded(child: Text("价格毫升"))),
                          DataColumn(label: Expanded(child: Text("成分"))),
                          DataColumn(label: Expanded(child: Text("评论总数"))),
                          DataColumn(label: Expanded(child: Text("操作")))
                        ],
                        rows: _dataRowList(viewModel),
                      ),
                    ),
                  ),
                ),
                Container(
                  color: Colors.black12,
                  child: Row(
                    children: [
                      IconButton(
                          onPressed: () => viewModel.addPage(false),
                          icon: Icon(Icons.keyboard_arrow_left)),
                      Container(
                        width: 25,
                        height: 25,
                        color: Colors.blue,
                        alignment: Alignment.center,
                        child: Text("${viewModel.page1}"),
                      ),
                      Container(
                        width: 25,
                        height: 25,
                        alignment: Alignment.center,
                        color: Colors.white,
                        child: Text("${viewModel.page2}"),
                      ),
                      IconButton(
                          onPressed: () => viewModel.addPage(true),
                          icon: Icon(Icons.keyboard_arrow_right))
                    ],
                  ),
                )
              ],
            ),
          );
        });
  }

  List<DataRow> _dataRowList(CommodityManagementViewModel viewModel) {
    List<DataRow> rows = [];
    if (viewModel.listShopAll.length != 0) {
      viewModel.listShopAll.forEach((element) {
        int kind = element["kind"];
        String kindStrType = "";
        brandTypeList.forEach((element) {
          if (element.value == kind) {
            kindStrType = element.label;
          }
        });
        rows.add(DataRow(
            onSelectChanged: (select) {
              viewModel.multipleChoice(element);
            },
            selected: viewModel.listShopAllSelect.contains(element),
            cells: [
              DataCell(SelectableText("${element["id"]}")),
              DataCell(SelectableText(element["brandName"])),
              DataCell(SelectableText(element["brandEnName"])),
              DataCell(SelectableText(kindStrType)),
              DataCell(SelectableText(element["series"])),
              DataCell(SelectableText(element["color"])),
              DataCell(SelectableText("￥${element["price"]}")),
              DataCell(SelectableText(element["ingredient"].toString())),
              DataCell(
                  SelectableText("${(element["comment"] as List).length}")),
              DataCell(Row(
                children: [
                  MaterialButton(
                      child: Text("编辑"),
                      textColor: Colors.white,
                      color: Colors.blue,
                      onPressed: () {
                        viewModel.editShopJump(element);
                      }),
                  MaterialButton(
                      child: Text("删除"),
                      textColor: Colors.white,
                      color: Colors.blue,
                      onPressed: () {
                        viewModel.deleteShop(element);
                      }),
                  MaterialButton(
                      child: Text("复制"),
                      textColor: Colors.white,
                      color: Colors.blue,
                      onPressed: () {
                        viewModel.copyShop(element);
                      }),
                  MaterialButton(
                      child: Text("评论"),
                      textColor: Colors.red,
                      color: Colors.blue,
                      onPressed: () {
                        viewModel.productReview(element);
                      })
                ],
              )),
            ]));
      });
    }
    return rows;
  }

  itemIput(String value, TextEditingController controller) {
    return Row(
      children: [
        Text(value),
        Container(
          margin: EdgeInsets.only(left: 10),
          width: 200,
          child: TextField(
            controller: controller,
            decoration: InputDecoration(hintText: "请输入关键词"),
            // keyboardType: TextInputType.visiblePassword,
            // obscureText: true,
            onChanged: (value) {},
          ),
        ),
      ],
    );
  }
}
